package com.hqyj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.UserSerInfoDao;
import com.hqyj.pojo.ManagerInfo;
import com.hqyj.pojo.Resultinfo;
import com.hqyj.pojo.UserSerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class UserSerServiceImpl implements UserSerService {
    @Autowired(required = false)
    UserSerInfoDao userSerInfoDao;
    @Override
    public HashMap<String, Object> select(Resultinfo user) {
        HashMap<String, Object> map =new HashMap<String, Object>();
        //根据用户的查询条件判断用户需要的查询
        System.out.print(user.getSerManager());
        //1 设置分页查询的页码，每页显示的行数
        PageHelper.startPage(user.getPage(),user.getRow());
        List<Resultinfo> list =new ArrayList<>();


        if(user.getConValue().equals("")){
            list = userSerInfoDao.select(user);
        }else {
            if (user.getCondition().equals("编号")) {
                //设置用户输入的查询条件
                user.setSerId(Integer.parseInt(user.getConValue()));
                list = userSerInfoDao.findByUserId(user);
                System.out.println(list+"oooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
            } else if (user.getCondition().equals("用户名")) {

                user.setSerName(user.getConValue());
                list = userSerInfoDao.findByUserName(user);
            }
            else if(user.getCondition().equals("服务类型")){
                user.setSerType(user.getConValue());
                list = userSerInfoDao.findByserTyped(user);

            }
            else if(user.getCondition().equals("经理")){
                user.setSerManager(user.getConValue());
                list = userSerInfoDao.findByserManager(user);
            }else
             {
                list = userSerInfoDao.select(user);
                System.out.println();
            }
        }
        //3 创建分页对象,查训的数据转换成对象
        PageInfo<Resultinfo> page = new PageInfo<Resultinfo>(list);
        //获取分页的当前页的集合
        map.put("list",page.getList());
        //总条数
        map.put("total",page.getTotal());

        //总页数
        map.put("totalPage",page.getPages());
        //上一页
        if(page.getPrePage()==0){
            map.put("pre",1);
        }else {
            map.put("pre", page.getPrePage());
        }
        //下一页,判断下一页的值是否大于等于最后一页
        if(page.getNextPage()==0){
            map.put("next", page.getPages());

        }else {
            map.put("next", page.getNextPage());
        }
        //当前页
        map.put("cur",page.getPageNum());
        return map;
    }

    @Override
    public String wanchen(Resultinfo user) {
        int n = userSerInfoDao.wanchen(user);
        if(n>0){
            return "保存成功";
    }
        else {
            return "保存失败";
        }

    }
    @Override
    public Resultinfo selectByUserId(Resultinfo user) {
        return userSerInfoDao.selectByUserId(user);
    }

    @Override
    public String del(Resultinfo user) {
        int num = userSerInfoDao.del(user);
        if(num>0){
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public String del1(Resultinfo user) {
        int num = userSerInfoDao.del1(user);
        if(num>0){
            return "删除成功";
        }
        return "删除失败";
    }

}
